
public class Recta {
	private double a;
	private double b;
	private double c;
	private Pnt p;
	private Pnt q;
	
	public Recta(Pnt p,Pnt q){
		this.a=-(q.coord(1)-p.coord(1));
		this.b=-(p.coord(0)-q.coord(0));
		this.c=-((q.coord(0)*p.coord(1))-(p.coord(0)*q.coord(1)));
		this.p=p;
		this.q=q;
	}
	public Pnt getP() {
		return p;
	}
	public void setP(Pnt p) {
		this.p = p;
	}
	public Pnt getQ() {
		return q;
	}
	public void setQ(Pnt q) {
		this.q = q;
	}
	public Recta(double a,double b,double c){
		this.a=a;
		this.b=b;
		this.c=c;
	}
	public String toString(){
		String s="Ecuacion general de la recta Ax+By+C=0\n";
		s+=a+"x";
		if(b>0){
			s+=" + "+b+"y";
		} else {
			s+=b+"y";
		}
		if(c>0){
			s+=" + "+c;
		} else {
			s+=c;
		}
		s+=" = 0";
		return s;
	}
	public double getA() {
		return a;
	}
	public void setA(double a) {
		this.a = a;
	}
	public double getB() {
		return b;
	}
	public void setB(double b) {
		this.b = b;
	}
	public double getC() {
		return c;
	}
	public void setC(double c) {
		this.c = c;
	}

	public double distanciaRecta(Recta r2){
		double num=Math.abs(r2.getC()-this.c);
		double den=Math.sqrt(Math.pow(this.a, 2)+Math.pow(this.b, 2));
		return num/den;
	}
	
	public double distanciaPunto(double x, double y){
		Recta perpendicular=this.perpendicularPunto(x, y);
		Pnt interseccion=this.interseccionRectaPerpendicular(perpendicular);
		double distancia=Math.sqrt(Math.pow((interseccion.coord(0)-x), 2)+Math.pow(interseccion.coord(1)-y, 2));
		return distancia;
	}
	
	public boolean contienePunto(Pnt pto){
		double resul=(this.a*pto.coord(0))+(this.b*pto.coord(1))+this.c;
		if(resul<0.5 && resul>-0.5)
			return true;
		else
			return false;
	}
	
	public double distanciaPuntoSegmento(double x,double y){
		if(this.p!=null && this.q!=null){
			Recta perpendicular=this.perpendicularPunto(x, y);
			Pnt interseccion=this.interseccionRectaPerpendicular(perpendicular);
			double denominador = (this.q.coord(0)-this.p.coord(0))*(this.q.coord(0)-this.p.coord(0))+(this.q.coord(1)-this.p.coord(1))*(this.q.coord(1)-this.p.coord(1));
			double u= ((interseccion.coord(0)-this.p.coord(0))*(this.q.coord(0)-this.p.coord(0))+(interseccion.coord(1)-this.p.coord(1))*(this.q.coord(1)-this.p.coord(1)))/denominador;
			//El punto proyectado pertenece al segmento
			if(u > 0.0 && u < 1.0) {
		           return this.distanciaPunto(x,y);
		    }
			//El punto proyectado no pertenece al segmento
			else {
				 return Math.min(Math.sqrt(Math.pow((this.p.coord(0)-x), 2)+Math.pow(this.p.coord(1)-y, 2)), Math.sqrt(Math.pow((this.q.coord(0)-x), 2)+Math.pow(this.q.coord(1)-y, 2)));
			}
		} else {
			return this.distanciaPunto(x,y);
		}
	}
	
	public Recta perpendicularPunto(double x, double y){
		double newa=this.b;
		double newb=-(this.a);
		double newc=((-(this.b))*x)+(this.a*y);
		Recta  aux=new Recta(newa,newb,newc);
		return aux;
	}
	
	public Pnt interseccionRectaPerpendicular(Recta r2){
		double newx=-(((this.a*this.c)+(this.b*r2.getC()))/(Math.pow(this.a, 2)+Math.pow(this.b, 2)));
		double newy=((this.a*r2.getC())-(this.b*this.c))/(Math.pow(this.a, 2)+Math.pow(this.b, 2));
		double[] coords=new double[2];
		coords[0]=newx;
		coords[1]=newy;
		return new Pnt(coords);
	}
	
	public static void main(String[] args){
		double[] p=new double[2];
		double[] q=new double[2];
		p[0]=1;
		p[1]=4;
		q[0]=5;
		q[1]=1;
		Pnt pn=new Pnt(p);
		Pnt qn=new Pnt(q);
		Recta r=new Recta(pn,qn);
		double[] pt=new double[2];
		pt[0]=1;
		pt[1]=4;
		Pnt ptn=new Pnt(pt);
		System.out.println(r.contienePunto(ptn));
	}
	
}
